package com.prs.crm.service.oa.impl;

import java.io.File;

import org.courser.Util;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.MailItemDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MailBox;
import com.prs.crm.domain.oa.MailItem;
import com.prs.crm.exception.InValidException;
import com.prs.crm.mail.MailSender;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.MailBoxService;
import com.prs.crm.service.oa.MailItemService;

public class MailItemServiceImpl extends BaseServiceImpl<MailItem,MailItemDao> implements MailItemService{

	@Autowired 
	private MailSender mailSender;
	
	@Autowired
	private MailBoxService mailBoxService;
	
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public MailSender getMailSender() {
		return mailSender;
	}
	
	public void setMailBoxService(MailBoxService mailBoxService) {
		this.mailBoxService = mailBoxService;
	}

	public MailBoxService getMailBoxService() {
		return mailBoxService;
	}

	public void executeSendMail(MailItem item,File[] files,String[] fileNames,Person sender)
	{
		MailBox box = this.getMailBoxService().findDefaultMailBox(sender);
		if (box==null)
			throw new InValidException("没有发现邮箱设置,请设置您的邮箱信息");
		try
		{
			if (files!=null)
			{
				for (int i=0;i<files.length;i++)
				{
					Attach at = FileUtil.getInstance().saveFile(files[i], fileNames[i], "", Constrains.UPLOAD_DIR);
					Attachment attachment = new Attachment();
					attachment.setPath(at.getPath());
					attachment.setName(fileNames[i]);
					attachment.setType(Constrains.ATTACHMENTTYPE_DISK);
					item.addAttach(attachment);
				}
			}
			item.setMailBox(box);
			item.setType(Constrains.MAILITEMTYPE_SEND);
			item.setSender(box.getUserName());
			item.setSendDate(Util.nowFullTime());
			this.getMailSender().sendMail(item);
			super.save(item);
		}
		catch (InValidException e)
		{
			throw e;
		}
	}

	@Override
	public int batchRemove(java.io.Serializable[] ids)
	{
		return this.getDao().updateSign(ids,Constrains.SIGN_DRAFT);
	}
	
	public void executeRead(MailItem item)
	{
		item.setReadStatus(Constrains.MAILITEMREAD_SEEN);
		super.save(item);
	}
	
	public boolean exist(MailBox box,String messageId,Person reciver)
	{
		return this.getDao().exist(box, messageId, reciver);
	}
	
	public PaginationSupport listInBox(DetachedCriteria criteria,PaginationSupport page,Person reciver)
	{
		criteria.add(Restrictions.eq("type", Constrains.MAILITEMTYPE_IN)).add(Restrictions.eq("reciver", reciver))
			.add(Restrictions.ne("sign", Constrains.SIGN_DRAFT));
		criteria.addOrder(Order.desc("id"));
		return super.listByPage(criteria, page);
	}
	
	public PaginationSupport listOutBox(DetachedCriteria criteria,PaginationSupport page,Person reciver)
	{
		criteria.add(Restrictions.eq("type", Constrains.MAILITEMTYPE_SEND)).add(Restrictions.eq("recorder", reciver))
			.add(Restrictions.ne("sign", Constrains.SIGN_DRAFT));
		criteria.addOrder(Order.desc("id"));
		return super.listByPage(criteria, page);
	}
}
